﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

using Spring.Core;
using Spring.Data;
using Spring.Web;
using Spring.Context;
using Spring.Objects.Factory;

namespace BookFinder
{
    public partial class _Default : System.Web.UI.Page 
    {
        private DataTable dt;
        string[] supportedSearchAPIs;
        IObjectFactory factory;
        //List<string> bookList = new List<string>();
        
        
        BooksCollection bookCollectionList;

        void CreateDataSource()
        {
            bookCollectionList = new BooksCollection();
            dt = new DataTable();

            dt.Columns.Add(new DataColumn("Source", typeof(string)));
            dt.Columns.Add(new DataColumn("Cover", typeof(string)));
            //dt.Columns.Add(new DataColumn("Vendor", typeof(string)));
            dt.Columns.Add(new DataColumn("BookName", typeof(string)));
            dt.Columns.Add(new DataColumn("BookPrice", typeof(string)));
            dt.Columns.Add(new DataColumn("Author", typeof(string)));
            dt.Columns.Add(new DataColumn("Publisher", typeof(string)));
            dt.Columns.Add(new DataColumn("Year", typeof(string)));
            dt.Columns.Add(new DataColumn("ISBN", typeof(string)));
           
            DataView dv = new DataView(dt);            
        }

        protected void Page_PreLoad(object sender, EventArgs e)
        {
                CreateDataSource();            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                if (Session["SearchResults"] != null)
                    SortLoadResults(ddl_OrderBy.SelectedValue, (BooksCollection)Session["SearchResults"]); 
            }
           
                //// Get the spring context which contains all of the defined objects
                //IApplicationContext ctx = Spring.Context.Support.ContextRegistry.GetContext();
                //factory = (IObjectFactory)ctx;
                //// Using reflection to get the object names defined
                //supportedSearchAPIs = ctx.GetObjectDefinitionNames();
            
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {

            // Get the spring context which contains all of the defined objects
            IApplicationContext ctx = Spring.Context.Support.ContextRegistry.GetContext();
            factory = (IObjectFactory)ctx;
            // Using reflection to get the object names defined
            supportedSearchAPIs = ctx.GetObjectDefinitionNames();

                if(searchText.Text.Length == 0)
                {
                    return;
                }
             
                ISearchAPI searchBooks = null;
                //Loop through the objects and then perform the search on each of the objects
                foreach (string searchAPI in supportedSearchAPIs)
                {
                    searchBooks = (ISearchAPI)factory.GetObject(searchAPI);
                    if (searchBooks != null)
                    {
                        searchBooks.sendSearchQuery(searchText.Text, ref bookCollectionList);
                    }                    
                }
            
          
                BooksCollectionCleanUp bcCleanup = new BooksCollectionCleanUp();
                bcCleanup.cleanUpCollectionList(ref bookCollectionList);

                Session["SearchResults"] = bookCollectionList;

                SortLoadResults(ddl_OrderBy.SelectedValue,bookCollectionList);
            
               
          
        }

        private void SortLoadResults(string orderBy,BooksCollection books)
        {
         
            books.sortBy(orderBy);

            repeater_BooksList.DataSource = books;
            repeater_BooksList.DataBind();
        }

        protected void ddl_OrderBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            //SortLoadResults(ddl_OrderBy.SelectedValue);
        }
    }
}
